using System;
using System.Threading;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Text;

/*
 * 0 =  메인화면
 * 10 = STAGE 1
 * 21 = STAGE 2_1
 * 22 = STAGE 2_2
 * 31 = STAGE 3_1
 * 32 = STAGE 3_2
 */
 
public class eduProgramClient : MonoBehaviour 
{
    private static eduProgramClient _instance;
    
    public static eduProgramClient Instance
    {
        get
        {
            if (_instance == null)
            {
                _instance =new eduProgramClient();
            }

            return _instance;
        }
    }
    
    public GameObject inputBox;
    public GameObject inputBox1;
    UIInput uiInput;
    UIInput uiInput1;
    private string idField;
    private string passField;
    public static string id;
    private static bool wrong = false;      
    
    public void getId()
    {
        uiInput = inputBox.GetComponent<UIInput>();
        idField = uiInput.label.text;
        string[] idField1 = idField.Split('|');
        idField = idField1[0];
        id = idField;
    }

    public void getPass()
    {
        uiInput1 = inputBox1.GetComponent<UIInput>();
        passField = uiInput1.label.text;
        string[] passField1 = passField.Split('|');
        passField = passField1[0];
        
    }
    
    public void connect()
    {
        
        string loginCheck = "USER " + idField + " " + passField;     
        msgHandle(loginCheck);                
        /*
        if (result == 230)
        {
            Application.LoadLevel("mainMenu");
        }
        else
        {
            wrong = true;
        }
         */
    }
    
    public void msgHandle(string input)
    {
        WWWForm form = new WWWForm();

        form.AddField("request_msg", input);

        WWW www = new WWW("220.67.124.155", form);        
        StartCoroutine(WaitForResponse(www));     
    }
    
    
    IEnumerator WaitForResponse(WWW www)
    {        
        yield return www;
        if (www.error == null)
        {
            Debug.Log("WWW ok! : " + www.text);
        }
        else
        {
            Debug.Log("WWW Error : " + www.error);
        }         

        string responseCode = www.text;
        string[] handle = null;
        handle = responseCode.Split(' ');        

        if(responseCode == "User Logged in")
            Application.LoadLevel("mainMenu");
        else if(responseCode == "Invalid ID or Password")
            //자기 화면 그대로 있기
            Application.LoadLevel("mainMenu"); // 임시방편
        else if (handle[0] == "Version")
        {
            if (handle[1] == "Permitted")
            {

            }
            else
            {

            }
        }else if (handle[0] == "GO")
        {
            int temp = int.Parse(handle[2]);
            switch (temp)
            {
                case 0:     // 메인화면
                    break;
                case 10:    // STAGE 1 
                    break;
                case 21:    // STAGE 2_1 
                    break;
                case 22:    // STAGE 2_2
                    break;
                case 31:    // STAGE 3_1
                    break;
                case 32:    // STAGE 3_2
                    break;
                default:
                    break;
            }
        }
        else
        {
            Application.Quit();
        }
    }    
    
    public void check_version()
    {        
        string check = "CHECK";
        msgHandle(check);        
    }    

    void OnGUI()
    {
        GUIStyle style = new GUIStyle();
        style.fontSize = 20;
        style.normal.textColor = Color.black;
        var stringToEdit = "ID or Pass is wrong!";
        if (wrong == true)
        {
            GUI.Label(new Rect(400, 305, 130, 50), stringToEdit, style);
        }        
    }
} 

